// FileName: GConfirm.ts
// ProjectName: miu-club-html
// 作者：区区电脑
// createTime: 2019/9/28
import GView from "./base/GView";
import {GColor, GFlex, GFlexAlignItems, GFlexJustifyContent, GPosition} from './base/GEnum'
import GButton from "./base/GButton";

export default class GConfirm {
    public confirm(title:string, message:string, btns: any[]):void {
        let view:GView = new GView();
        let subview:GView = new GView();
        let titleView:GView = new GView();
        let messageView:GView = new GView();
        let btnGroupView:GView = new GView();

        subview.addChild(titleView);
        subview.addChild(messageView);
        subview.addChild(btnGroupView);

        view.addChild(subview);
        view.addToWindow();

        // 遮罩
        view.position = GPosition.fixed;
        view.left = 0;
        view.right = 0;
        view.bottom = 0;
        view.top = 0;
        view.id = 'cover';
        view.zIndex = 999;
        view.backgroundColor = 'rgba(0, 0, 0, 0)';
        view.flexType = GFlex.column;
        view.justifyContent = GFlexJustifyContent.center;
        view.alignItems = GFlexAlignItems.center;
        view.reset();
        view.animation(0.3, (v:GView) => {
            v.backgroundColor = 'rgba(0, 0, 0, 0.5)';
        }, () => {
        });

        // 弹框
        subview.width = 300;
        subview.height = 180;
        subview.backgroundColor = 'rgba(255, 255, 255, .95)';
        subview.margin = view.marinInit(-50, 0, 0, 0);
        subview.borderRadius = 5;
        subview.flexType = GFlex.column;
        subview.justifyContent = GFlexJustifyContent.between;
        subview.alignItems = GFlexAlignItems.center;
        subview.padding = subview.paddingInit(20, 0, 10, 0);
        subview.transform = 'scale(0)';
        subview.reset();
        subview.animation(0.2, (v:GView) => {
            v.transform = 'scale(1)';
        });

        // 标题
        titleView.text = title;
        titleView.fontSize = 18;
        titleView.reset();

        // 提示消息
        messageView.text = message;
        messageView.flex = 1;
        messageView.padding = messageView.paddingInit(20, 0, 0, 0);
        messageView.color = '#999999';
        messageView.reset();

        // 按钮组
        btnGroupView.height = 40;
        btnGroupView.width = 300;
        btnGroupView.flexType = GFlex.row;
        // btnGroupView.justifyContent = GFlexJustifyContent.between;
        btnGroupView.alignItems = GFlexAlignItems.center;
        for (let i = 0; i < btns.length; i++) {
            let btn:GButton = new GButton();
            btn.text = btns[i].text;
            btn.flex = 1;
            btn.color = btns[i].color;
            btn.backgroundColor = GColor.transparent;
            btn.onClick = () => {
                if (btns[i].callback) {
                    btns[i].callback();
                }
                subview.animation(0.2, (v:GView) => {
                    v.transform = 'scale(0)';
                });
                view.animation(0.3, (v:GView) => {
                    v.backgroundColor = 'rgba(0, 0, 0, 0)';
                }, (v:GView) => {
                    v.delete();
                });
            };
            btnGroupView.addChild(btn);
            if (i !== btns.length - 1) {
                let line:GView = new GView();
                line.height = 20;
                line.width = 1;
                line.backgroundColor = '#999999';
                btnGroupView.addChild(line);
            }
        }
        btnGroupView.reset();
    }
}
